<?php

namespace Modules\Agent\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;

use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

// use PHPExcel_Cell_DataType;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

use Modules\Activity\Entities\ActivityOrder;
use Modules\Asset\Entities\PackageOrder;
use Modules\Order\Entities\ProductOrder;

use Modules\User\Entities\User;

class AgentExport implements WithColumnFormatting, FromCollection, WithTitle, WithHeadings, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $list;

    public function __construct($list)
    {
        $this->list = $list;
    }

    public function columnFormats(): array
    {
        return [];
        // return [
        //     'O' => DataType::TYPE_STRING
        // ];
    }

    /**
     * @return Builder
     */
    public function collection()
    {
        return $this->list;
        // return BookInfo::where('booking_session_id', $this->session->id)->displayed();
    }

    /**
     * @return string
     */
    public function title(): string
    {
        return '分销员导出';
    }

    public function map($item): array
    {

        $res = [
            $item->id,
            $item->name,
            // $order->number,
            $item->phone,
            $item->status === 1 ? '正常' : '已禁用',
            $item->direct_invitees_count,
            $item->indirect_invitees_count,
            $item->completed_money_brokerage / 100 ?: 0,
            $item->completed_score_brokerage ?: 0,
            $item->pending_money_brokerage / 100 ?: 0,
            $item->pending_score_brokerage ?: 0,

            $item->asset('score')->balance,
            ProductOrder::paid()->where('user_id', $item->id)->count(),
            ProductOrder::paid()->where('user_id', $item->id)->sum('product_money_price') / 100,
            ProductOrder::paid()->where('user_id', $item->id)->sum('pay_money_price') / 100,
            ActivityOrder::paid()->where('user_id', $item->id)->where('node_type', 'fudai')->count(),
            ActivityOrder::paid()->where('user_id', $item->id)->where('node_type', 'fudai')->sum('product_money_price') / 100,
            ActivityOrder::paid()->where('user_id', $item->id)->where('node_type', 'fudai')->sum('pay_money_price') / 100,
            PackageOrder::paid()->where('user_id', $item->id)->count(),

            ProductOrder::paid()->whereHas('user', function ($query) use ($item) {
                return $query->where('inviter_id', $item->id)->orWhere('indirect_inviter_id', $item->id);
            })->count(),
            ProductOrder::paid()->whereHas('user', function ($query) use ($item) {
                return $query->where('inviter_id', $item->id)->orWhere('indirect_inviter_id', $item->id);
            })->sum('product_money_price') / 100,
            ProductOrder::paid()->whereHas('user', function ($query) use ($item) {
                return $query->where('inviter_id', $item->id)->orWhere('indirect_inviter_id', $item->id);
            })->sum('pay_money_price') / 100,

            ActivityOrder::paid()->whereHas('user', function ($query) use ($item) {
                return $query->where('inviter_id', $item->id)->orWhere('indirect_inviter_id', $item->id);
            })->where('node_type', 'fudai')->count(),
            ActivityOrder::paid()->whereHas('user', function ($query) use ($item) {
                return $query->where('inviter_id', $item->id)->orWhere('indirect_inviter_id', $item->id);
            })->where('node_type', 'fudai')->sum('product_money_price') / 100,
            ActivityOrder::paid()->whereHas('user', function ($query) use ($item) {
                return $query->where('inviter_id', $item->id)->orWhere('indirect_inviter_id', $item->id);
            })->where('node_type', 'fudai')->sum('pay_money_price') / 100,

            PackageOrder::paid()->whereHas('user', function ($query) use ($item) {
                return $query->where('inviter_id', $item->id)->orWhere('indirect_inviter_id', $item->id);
            })->count(),
            $item->created_at
        ];

        return $res;
    }

    public function headings(): array
    {
        return [
            '唯一ID',
            '用户名',
            // '订单状态',
            // '订单编号',
            // '订单总金额',
            // '微信支付金额',
            '手机号',
            '用户状态',
            '直接粉丝数',
            '间接粉丝数',
            '已结算佣金(人民币)',
            '已结算佣金(积分)',
            '待结算佣金(人民币)',
            '待结算佣金(积分)',

            '总积分',
            '商城单(本人)',
            '商城单总金额(本人)',
            '商城单微信支付(本人)',
            '盲盒单(本人)',
            '盲盒单总金额(本人)',
            '盲盒单微信支付(本人)',
            '提货单(本人)',
            '商城单(粉丝)',
            '商城单总金额(粉丝)',
            '商城单微信支付(粉丝)',
            '盲盒单(粉丝)',
            '盲盒单总金额(粉丝)',
            '盲盒单微信支付(粉丝)',
            '提货单(粉丝)',
            '注册时间'
        ];
    }
}
